<?php

/**
 * @see Workset_Sphinx_Query_Interface
 */
require_once 'Workset/Sphinx/Query/Interface.php';


class Workset_Sphinx_Query implements Workset_Sphinx_Query_Interface {

    protected $_indexes = array();

    protected $_matches = array();

    protected $_attributes = array();

    protected $_order = array();

    protected $_limit = null;

    protected $_offset = 0;

    protected $_select = array('*');

    protected $_groupBy = array();

    protected $_options = array();

    public function addIndex($index) {

        if (!in_array($index, $this->_indexes)) {
            $this->_indexes[] = (string)$index;
        }
        return $this;
    }

    public function setIndexes(array $indexes) {
        $this->_indexes = array();

        foreach ($indexes as $index) {
            $this->addIndex($index);
        }

        return $this;
    }

    public function getIndexes() {
        return $this->_indexes;
    }

    public function setMatches(array $matches) {
        $this->_matches = $matches;
        return $this;
    }

    public function getMatches() {
        return $this->_matches;
    }

    public function setAttributes(array $attributes) {
        $this->_attributes = $attributes;
        return $this;
    }

    public function getAttributes() {
        return $this->_attributes;
    }

    function setOrder(array $order) {
        $this->_order = $order;
        return $this;
    }

    function getOrder() {
        return $this->_order;
    }

    function setLimit($limit) {
        if ($limit > 0) {
            $this->_limit = (int)$limit;
        } else {
            throw new Zend_Exception('limit must be greater than 0');
        }
        return $this;
    }

    function getLimit() {
        return $this->_limit;
    }

    function setOffset($offset) {
        if ($offset >= 0) {
            $this->_offset = (int)$offset;
        } else {
            throw new Zend_Exception('offset must be 0 or greater than 0');
        }
        return $this;
    }

    function getOffset() {
        return $this->_offset;
    }

    function setGroupBy(array $fields) {
        $this->_groupBy = $fields;
    }

    function getGroupBy() {
        return $this->_groupBy;
    }

    function setSelect(array $expr) {
        if ($expr) {
            $this->_select = $expr;
        } else {
            throw new Zend_Exception('select expression is empty');
        }
        return $this;
    }

    function getSelect() {
        return $this->_select;
    }

    function setOptions(array $options) {
        $this->_options = $options;
    }

    function getOptions() {
        return $this->_options;
    }

}